<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Menu;
use app\admin\model\Role;
use app\admin\model\Admin;
use app\admin\model\Student;
use app\admin\model\Zxlog;
use think\Db;
class Home extends Base
{
    public $zxs_id = array();
    /*
     * 后台首页
     */
    public function index()
    {
        $user = $this->user_info();  //当前登录用户信息

        if($user['roles_id']==6){ //如果当前用户是咨询顾问则不需要查找子级
            $consultant = [$user['id']];
        }else{
            $list = $this->getMyTeamUserArr($user['id']);
            $this->UserResAll($list);
            $consultant = $this->zxs_id; 
        }
        $getDataType = input('get.type');

        if(input('?get.type') && $getDataType == 'status_count'){
        //沉默人数
        $cm_count = Db::table('student')->where('sstatus',0)->where('consultant','in',$consultant)->count();

        //正在咨询的人数
        $zxz_count = Db::table('student')->where('sstatus',2)->where('consultant','in',$consultant)->count();

        //今日签约
        $signing_day_count  = Db::table('enrollrecord')->whereTime('create_time', 'today')->where('signing_consultant','in',$consultant)->count();

        //本月签约
        $signing_month_count= Db::table('enrollrecord')->whereTime('create_time', 'month')->where('signing_consultant','in',$consultant)->count();
            return json_encode(array(
                'cm_count'=>$cm_count,
                'zxz_count'=>$zxz_count,
                'signing_day_count'=>$signing_day_count,
                'signing_month_count'=>$signing_month_count,
            ));
        }


            // // 往期数据
            // for($i=0;$i<=6;$i++){
            //     $first_day= date('Y-m-d', mktime(0,0,0,date('m')-$i,1,date('Y'))); //月份的第一天
            //     $daynum= date('t',strtotime("$first_day"));  //次月的天数
            //     $last_day = date('Y-m-d', mktime(0,0,0,date('m')-$i,$daynum,date('Y'))); //月份的最后一天

            //     //查询签约数据
            //     $signing_count_Past = Db::table('enrollrecord')->whereTime('create_time', 'between', [$first_day, $last_day])->count();
                
            //     //查询沉默数据
            //     $cm_count_Past = Db::table('student')->where('sstatus',0)->whereTime('status_time', 'between', [$first_day, $last_day])->count();

            //     //x轴时间
            //     $time = date('Y-m',mktime(0,0,0,date('m')-$i,1,date('Y')));
            //     $time = strtotime($time."UTC")*  1000;  

            //     //保存数据
            //     $past_data ['signing'][] = [$time,$signing_count_Past]; //往期签约
            //     $past_data ['cm'][] = [$time,$cm_count_Past]; //往期沉默
            // }
 


        // 业绩统计
        $yj =[];  //业绩数组
        foreach($consultant as $k=>$v){
            $zyyj = db('student')  //查询主要业绩
            ->where(array('s.consultant'=>$v,'s.sstatus'=>1))
            ->where('s.consultant = s.signing_consultant')
            ->where('s.consultant != -1')
            ->alias('s')
            ->join('enrollrecord e', 's.sid = e.resid')
            // ->group('bind_enroll')
            ->field('count(sid) as sum, count(e.registeryfee) as qyje')
            ->select();
           
            $user = Db::name('admin')->where('id',$v)->find();
            $user['zyyj'] = isset($zyyj['sum'])? $zyyj['sum'] : 0;
            $user['qyje'] = isset($zyyj['qyje'])? $zyyj['sum'] : 0; //签约金额
            $yj[]=$user;
        }
   

        //今日录入的数据
        if(input('?get.type') && $getDataType == 'enter_today'){
            $enter_today = Db::table('student')->whereTime('create_time', 'today')->field('sid,sname,zx_form,infosource,source_details')->select();
            return json_encode($enter_today);
        }
        //回访情况
        //今日已回访
        $returned = collection(Zxlog::alias('z')
        ->join('student s', 'z.res_id = s.sid','left')
        ->join('admin a', 's.consultant = a.id','left')
        ->field('z.*, s.sname, s.consultant,a.username consultant_name,a.id aid')
        ->whereTime('z.create_time', 'today')
        ->select() )->toArray();
            
        $returned = delDataOnConsultant($returned, 'consultant',$consultant);


        $returned_today = array();
        foreach ($returned as $key => $list) {
            if(isset($returned_today[$list['consultant_name']])){
                $returned_today[$list['consultant_name']][] = $list;
            } else {
                $returned_today[$list['consultant_name']] = array();
                $returned_today[$list['consultant_name']][] = $list;
            }
        }
        
        
        // $yesterday_time = strtotime(date("Y-m-d",strtotime("-1 day"))); //昨天的日期
        //昨天的咨询记录id
        // $zxlog_yesterday = Zxlog::where('create_time','between time', [$yesterday_time, $yesterday_time+3600*24])->group('res_id')->column('res_id');
        //今天的咨询记录id
        // $zxlog_today = Zxlog::where('create_time','between time', [$yesterday_time+3600*24,$yesterday_time+3600*48])->group('res_id')->column('res_id');


        //今日未回访

        $returnvisit_today = Student::
        where('consultant','in',$consultant)
        ->whereTime('returnvisit_time', 'today')
        ->select();


        //昨日未回访
        $returnvisit_yesterday = Student::where(array(
            'sstatus'=> '2',
            // 'consultant'=>['in', $consultant],
            // 'sid'=>['not in', $zxlog_yesterday],
            'returnvisit_time'=>date("Y-m-d",strtotime("-1 day")),
        ))->select();
        $returnvisit_yesterday = delDataOnConsultant($returnvisit_yesterday, 'consultant',$consultant);

        $data = array(  //模板数据
            // 'cm_count' => $cm_count,  //沉默数
            // 'zxz_count' => $zxz_count,  //咨询中
            // 'signing_day_count' => $signing_day_count, //今日签约
            // 'signing_month_count' => $signing_month_count, //本月签约
            // 'past_data'  => json_encode($past_data), //往期数据
            'yj' => $yj,  //业绩统计
            // 'enter_today'=> json_encode($enter_today),  //今日录入的数据
            'returnvisit_yesterday'=>json_encode($returnvisit_yesterday), //昨日应该回访但未回访
            'returned_today' => $returned_today,
            'returnvisit_today'=> json_encode($returnvisit_today)
        );
        $this->assign($data);
        return  $this->fetch();
    }


    //查找当前登录用户下的所有咨询师id
    public function UserResAll($arr){
        foreach ($arr as $k => $v) {
            if($v['roles_id']==6)
                $this->zxs_id[]=$v['id'];
            $this->UserResAll($v['child']);
        }
        return false;
    }


    /**
     * getMyTeamUserArr 获取传入id 的所有现金咨询师或咨询主管
     * @param int $id   用户id
     * @return array    返回查询到的成员 组成的数组
     */
    public  function  getMyTeamUserArr($id) 
    {
        $roles_id_arr = [6,7]; //用于规定要查询的角色
        $u = Admin::where(['id' => $id])->field("id,username,photo,roles_id,pid")->find();
        
        $child = Admin::where(['pid' => $u['id']])
            ->where('roles_id', 'in', $roles_id_arr)
            ->field("id,username,photo,roles_id,pid")->select();
        foreach ($child as $k => $v) {
            $child[$k]['child'] = $this->getMyTeamUserArr($v['id']);
        }

        return collection($child)->toArray();
    }





}
